package interfaceptor;

import cn.china.xfood.service.WebInfoService;
import entity.Webinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;


public class MyInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {




        return true;
    }


    @Autowired
    WebInfoService webInfoService;

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

        Date date = new Date();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM");
        String format = sim.format(date);



        Webinfo webinfo = webInfoService.selectByTime(format);



        if (webinfo!=null){
            webinfo.setVisittimes(webinfo.getVisittimes()+1);
            webInfoService.updateByPrimaryKey(webinfo);
        }else {
            webInfoService.insert(new Webinfo(format,1));
        }

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {


    }
}
